using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// Effect base class
public abstract class Effect : MonoBehaviour
{
    private ParticleSystem particleSys;
    private Transform _target;
    public Action<Effect> BeginCallback{get => _beginCallback; set => _beginCallback = value;}
    public Action<Effect> EndCallback{get => _endCallback; set => _endCallback = value;}
    private Action<Effect> _beginCallback;
    private Action<Effect> _endCallback;
    private EffectConfigItem _cfg;
    private float _delay = 0.0f;    // 延迟倒计时
    private float _duration = 0.0f; // 持续时间
    private bool _isInit = false;
    private bool _isStart = false;
    private bool _isPlaying = false;
    private bool _isDelay = false;  // 是否延迟播放 -1为无延迟
    private bool _isDuration = false;   // 是否有持续时间 -1为循环特效
    private Transform _bindPoint;
    private bool _isEnd = false;
    public bool IsEnd{get => _isEnd;}
    public void Init(EffectConfigItem cfg)
    {
        _cfg = cfg;
        _delay = _cfg.delay;
        _duration = _cfg.duration;
        _isDelay = _cfg.delay > 0;
        _isDuration = _cfg.duration > 0;
        _isInit = true;
        _isEnd = false;
        SetEffectCfg(cfg);
        particleSys = GetComponent<ParticleSystem>();
    }

    public void SetEffectCfg(EffectConfigItem cfg)
    {
        _cfg = cfg;
        _delay = _cfg.delay;
    }

    public string GetEffectName()
    {
        return _cfg.name;
    }

    public int GetEffectId()
    {
        return _cfg.id;
    }

    public virtual void Play(){
        if(_isInit == false){
            Debug.LogError($"Effect.Play: effect {_cfg.id} is not init");
            return;
        }
        _isStart = true;
        //检查配置是否延迟播放 如果是则延迟播放 则屏蔽特效
        if(_delay > 0){
            // _isStart = true;
            if(gameObject.activeSelf == true)
            {
                gameObject.SetActive(false);
            }
        }else{
            // _delay = _cfg.delay;
            //配置时间为-1 则为循环特效 即时播放
            if(gameObject.activeSelf == false)
            {
                gameObject.SetActive(true);
            }
            if(particleSys != null){
                particleSys.Clear();
                particleSys.Play();
            }
            _isPlaying = true;
            _beginCallback?.Invoke(this);
        }

    }
    public virtual void Stop(){
        _isPlaying = false;
        _isStart = false;
        _isInit = false;
        _isDelay = false;
        _isDuration = false;
        _isEnd = true;

        _endCallback?.Invoke(this);

        transform.SetParent(null);
        _bindPoint = null;
        _target = null;

        if(particleSys != null){
            particleSys.Stop();
        }

        if(gameObject.activeSelf == true)
        {
            gameObject.SetActive(false);
        }
        // EffectMgr.Instance.StopEffect(this);

    }

    public void Refresh(){
        if(_cfg == null){
            return;
        }
        

        if(_isStart == false) return;
        //延迟检查
        if(_isDelay && _delay > 0){
            _delay -= Time.deltaTime;
            if(_delay <= 0){
                Play();
            }
        }
        if(_isPlaying == false) return;
        //有持续时间检查
        
        if(_isDuration &&_duration > 0){
            _duration -= Time.deltaTime;
            if(_duration <= 0){
                Stop();
            }
        }

    }

    public void SetTarget(Transform target)
    {
        _target = target;
        if(_cfg.BindPoint != ""){
            _bindPoint = target.Find(_cfg.BindPoint);
        }

        if(_bindPoint != null){
            transform.SetParent(_bindPoint);
        }else{
            transform.SetParent(_target);
        }
    }

    public void SetPosition(Vector3 position)
    {
        transform.position = position;
    }
    public void SetScale(float scale)
    {
        transform.localScale = Vector3.one * scale;
    }

    public void SetRotation(Quaternion rotation)
    {
        transform.rotation = rotation;
    }

}